/* 
This do file calculates the state-level weighted average amount of generation given current installations in each zip.
NOTES: All directories and paths should be set in the main "SetUpSubsidyCalcs.do" file.

*/

clear all

import delimited `"${data_path}/Generation_and_damages_withCO2_2021-03-05_eGrid_SR.csv"'

rename v7 inter
rename zip zipcode

merge 1:1 zipcode using `"${data_intermediate}/NumberSystems.dta"'
*Note: We have 1478 zipcodes with solar panels but no damage calculations.
*So I assign damages to these zipcodes based on the damages from the previous zipcode (when zipcodes are listed in numerical order)
replace total_generation = total_generation[_n-1] if _merge==2
drop _merge

*drop Puerto Rico, Hawaii, and Alaska
drop if state=="HI" | state=="AK" | state=="PR"

gen zip_total_gen = num_systems*total_generation

sort state
bysort state: egen state_num_systems = sum(num_systems)
bysort state: egen state_total_gen = sum(zip_total_gen)

gen state_wtd_avg_gen = state_total_gen/state_num_systems

collapse (mean) state_wtd_avg_gen, by(state)

*outsheet state state_wtd_avg_gen using `"${data_intermediate}/state_wtd_avg_gen.csv"', comma replace
export excel using `"${scripts_subsidies}/Solar_Incentives_Data_no_severin_data.xlsx"', sheet("System_Gen_CEC") sheetmodify cell(A2) keepcellfmt

